package com.dldata.devtools30.utils;


import lombok.Data;

import java.io.Serializable;

/**
 *   接口返回数据格式
 * @author
 * @email
 * @date  2019年1月19日
 */
@Data
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	private String msg = "操作成功！";

	/**
	 * 返回代码
	 */
	private Integer code = 0;
	
	/**
	 * 返回数据对象 data
	 */
	private T data;

	/**
	 * 返回数据数量 count
	 */
	private Integer count = 0;
	
	/**
	 * 时间戳
	 */
	private long timestamp = System.currentTimeMillis();

	public Result() {
		
	}

	
	public Result<T> error500(String message) {
		this.msg = message;
		this.code = 500;
		this.success = false;
		return this;
	}
	
	public Result<T> success(String message) {
		this.msg = message;
		this.code = 200;
		this.success = true;
		return this;
	}
	
	
	public static Result<Object> ok() {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(0);
		r.setMsg("成功");
		return r;
	}
	
	public static Result<Object> ok(String msg) {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(0);
		r.setMsg(msg);
		return r;
	}
	
	public static Result<Object> ok(Object data) {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(0);
		r.setData(data);
		return r;
	}

	public static Result<Object> ok(String msg,Object data){
		Result<Object> r=new Result<Object>();
		r.setSuccess(true);
		r.setMsg(msg);
		r.setCode(0);
		r.setData(data);
		return r;
	}

	public static Result<Object> ok(Object data,int count){
		Result<Object> r=new Result<Object>();
		r.setSuccess(true);
		r.setCount(count);
		r.setCode(0);
		r.setData(data);
		return r;
	}
	
	public static Result<Object> error(String msg) {
		if(StringUtil.isNullOrSpace(msg)){
			msg="服务器异常，请稍后再试";
		}
		return error(500, msg);
	}
	
	public static Result<Object> error(int code, String msg) {

		Result<Object> r = new Result<Object>();
		r.setCode(code);
		r.setMsg(msg);
		r.setSuccess(false);
		return r;
	}
	
	/**
	 * 无权限访问返回结果
	 */
	public static Result<Object> noauth(String msg) {

		return error(500, msg);
	}
}